// Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
// License: GNU General Public License v3. See license.txt

frappe.query_reports["Profit and Loss Statement CN"] = {
    "filters": [
        {
            "fieldname": "company",
            "label": __("Company"),
            "fieldtype": "Link",
            "options": "Company",
            "default": frappe.defaults.get_user_default("Company"),
            "reqd": 1
        },
        {
            "fieldname": "fiscal_year",
            "label": __("Fiscal Year"),
            "fieldtype": "Link",
            "options": "Fiscal Year",
            "default": frappe.defaults.get_user_default("fiscal_year"),
            "reqd": 1
        },
        {
            "fieldname": "period_start_date",
            "label": __("Start Date"),
            "fieldtype": "Date",
            "reqd": 1,
            "depends_on": "eval:doc.fiscal_year",
            "get_query": function() {
                var fiscal_year = frappe.query_report.get_filter_value('fiscal_year');
                if (fiscal_year) {
                    return {
                        "filters": {
                            "fiscal_year": fiscal_year
                        }
                    }
                }
            }
        },
        {
            "fieldname": "period_end_date",
            "label": __("End Date"),
            "fieldtype": "Date",
            "reqd": 1,
            "depends_on": "eval:doc.fiscal_year",
            "get_query": function() {
                var fiscal_year = frappe.query_report.get_filter_value('fiscal_year');
                if (fiscal_year) {
                    return {
                        "filters": {
                            "fiscal_year": fiscal_year
                        }
                    }
                }
            }
        },
        {
            "fieldname": "periodicity",
            "label": __("Periodicity"),
            "fieldtype": "Select",
            "options": ["Monthly", "Quarterly", "Half-Yearly", "Yearly"],
            "default": "Monthly"
        },
        {
            "fieldname": "compare_with_previous_year",
            "label": __("Year on Year"),
            "fieldtype": "Check"
        },
        {
            "fieldname": "compare_with_budget",
            "label": __("Against Budgeting"),
            "fieldtype": "Check"
        },
        {
            "fieldname": "budget",
            "label": __("Budget"),
            "fieldtype": "Link",
            "options": "Budget",
            "depends_on": "eval:doc.compare_with_budget == 1"
        },
        {
            "fieldname": "cost_center",
            "label": __("Cost Center"),
            "fieldtype": "Link",
            "options": "Cost Center",
            "depends_on": "eval:doc.compare_with_budget == 1",
            "get_query": function() {
                // 限制成本中心为当前选中公司的数据
                var company = frappe.query_report.get_filter_value('company');
                if (company) {
                    return {
                        "filters": {
                            "company": company,
                            "is_group": 0  // 只显示末级成本中心
                        }
                    }
                }
            }
        },
        {
            "fieldname": "hide_chart",
            "label": __("Hide Chart"),
            "fieldtype": "Check",
            "default": 0
        }
    ],
    
    "onload": function(report) {
        report.page.add_inner_button(__("Set Default Filters"), function() {
            // 设置默认过滤器值
            frappe.query_report.set_filter_value('company', frappe.defaults.get_user_default("Company"));
            frappe.query_report.set_filter_value('fiscal_year', frappe.defaults.get_user_default("fiscal_year"));
            
            // 如果有默认会计年度，获取其日期范围
            if (frappe.query_report.get_filter_value('fiscal_year')) {
                frappe.call({
                    method: "frappe.client.get_value",
                    args: {
                        doctype: "Fiscal Year",
                        filters: {name: frappe.query_report.get_filter_value('fiscal_year')},
                        fieldname: ["year_start_date", "year_end_date"]
                    },
                    callback: function(r) {
                        if (r.message) {
                            frappe.query_report.set_filter_value('period_start_date', r.message.year_start_date);
                            frappe.query_report.set_filter_value('period_end_date', r.message.year_end_date);
                        }
                    }
                });
            }
        });
    },
    
    "formatter": function(value, row, column, data, default_formatter) {
        value = default_formatter(value, row, column, data);
        
        // 为差异值添加颜色标识
        if (column.fieldname.includes("difference_")) {
            if (data[column.fieldname] > 0) {
                value = `<span style="color:green">${value}</span>`;
            } else if (data[column.fieldname] < 0) {
                value = `<span style="color:red">${value}</span>`;
            }
        }
        
        // 为百分比添加颜色标识
        if (column.fieldname.includes("percentage_change_")) {
            if (data[column.fieldname] > 0) {
                value = `<span style="color:green">${value}</span>`;
            } else if (data[column.fieldname] < 0) {
                value = `<span style="color:red">${value}</span>`;
            }
        }
        
        return value;
    }
};
